package com.zerdoor.service.impl;

import com.zerdoor.dao.RbacResourcesDao;
import com.zerdoor.service.RbacResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2015/2/2.
 */
@Service
public class RbacResourcesServiceImpl implements RbacResourcesService {
    @Autowired
    private RbacResourcesDao rbacResourcesDao;

    /**
     * 查询符合条件的所有资源
     *
     * @param parentId 父类编号
     * @return
     */
    @Override
    public List<Map<String, Object>> getRbacResources(int parentId) {
        return rbacResourcesDao.getRbacResources(parentId);
    }

    /**
     * 添加资源
     *
     * @param map
     * @throws Exception
     */
    @Override
    public void addRbacResources(Map<String, Object> map) throws Exception {
        rbacResourcesDao.addRbacResources(map);
    }

    /**
     * 编辑资源
     *
     * @param map
     * @throws Exception
     */
    @Override
    public void updateRbacResources(Map<String, Object> map) throws Exception {
        rbacResourcesDao.updateRbacResources(map);
    }

    /**
     * 删除资源
     *
     * @param resourcesId 资源编号
     * @throws Exception
     */
    @Override
    public void deleteRbacResources(int resourcesId) throws Exception {
        Map<String,Object> map = rbacResourcesDao.getResourcesByPatentId(resourcesId);
        if(Integer.parseInt(map.get("total").toString()) >0){
            throw new Exception("此类下面有子类！删除失败。");
        }else{
            rbacResourcesDao.deleteRbacResources(resourcesId);
        }
        rbacResourcesDao.deleteRbacResources(resourcesId);
    }
}
